package de.lang.rollo.controller.managed;

import java.net.UnknownHostException;







import org.mongojack.JacksonDBCollection;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.yammer.dropwizard.lifecycle.Managed;

import de.lang.rollo.controller.configurations.GpioConfiguration;
import de.lang.rollo.controller.model.RolloEvent;
import de.lang.rollo.controller.model.RolloMotor;
import de.lang.rollo.controller.model.Schalter;
import de.lang.rollo.controller.model.daos.EventDAO;
import de.lang.rollo.controller.model.daos.MotorDAO;
import de.lang.rollo.controller.model.daos.SchalterDAO;

public class MongoManaged implements Managed {

	private Mongo m;

	private static JacksonDBCollection<RolloEvent, String> events;
	private static JacksonDBCollection<RolloMotor, String> motoren;
	private static JacksonDBCollection<Schalter, String> schalter;
	
	private static EventDAO eventDao;
	private static MotorDAO motorDao;
	private static SchalterDAO schalterDao;
	

	public MongoManaged(GpioConfiguration configuration)
			throws UnknownHostException {
		this(configuration
				.getDatabaseConfiguration().getMongoHost(),configuration
				.getDatabaseConfiguration().getMongoPort(),configuration.getDatabaseConfiguration()
				.getMongoDb());
		
	}
		
		public MongoManaged(String host, int port, String database)
				throws UnknownHostException {

		// DB
		MongoClient mongoClient = new MongoClient(host, port);
		DB db = mongoClient.getDB(database);

		events = JacksonDBCollection.wrap(db.getCollection("events"),
				RolloEvent.class, String.class);
		motoren = JacksonDBCollection.wrap(db.getCollection("motoren"),
				RolloMotor.class, String.class);
		schalter = JacksonDBCollection.wrap(db.getCollection("schalter"),
				Schalter.class, String.class);

		this.m = mongoClient;
	}

	public void start() throws Exception {
	}

	public void stop() throws Exception {
		if (m != null)
			m.close();
	}

	public static JacksonDBCollection<RolloEvent, String> getEvents() {
		return events;
	}

	public static JacksonDBCollection<RolloMotor, String> getMotoren() {
		return motoren;
	}

	public static JacksonDBCollection<Schalter, String> getSchalter() {
		return schalter;
	}
	
	public static void dropAll(){
		events.drop();
		schalter.drop();
		motoren.drop();
	}

}